package cn.xyy.system.service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import cn.xyy.system.dao.DepartmentDao;
import cn.dev33.satoken.SaManager;
import cn.xyy.common.domain.system.auth.Department;
import cn.xyy.common.exception.AuthException;

/**
 * 部门业务层
 * 
 * @author xyy
 *
 */
@Service
public class DepartmentService {
	@Autowired
	private DepartmentDao departmentDao;

	public static String deptKey() {
		return ":department_key";
	}

	public List<Department> getAllDeptList() {
		List<Department> allDeptList = JSONArray.parseArray(SaManager.getSaTokenDao().get(deptKey()), Department.class);
		if (allDeptList == null || allDeptList.isEmpty()) {
			allDeptList = departmentDao.selectAllList();
			SaManager.getSaTokenDao().set(deptKey(), JSON.toJSONString(allDeptList), TimeUnit.DAYS.toMillis(7));
		}
		return allDeptList;
	}

	public List<Department> getEnableDeptList() {
		return getAllDeptList().stream().filter(e -> 1 == e.getStatus()).collect(Collectors.toList());

	}

	public List<Department> queryAllCompany() {
		return getEnableDeptList().stream().filter(e -> 1 == e.getLevels()).collect(Collectors.toList());
	}

//    public List<Department> queryAllChild(Integer parentId) {
//        List<Department> deptList = new ArrayList<Department>();
//
//        List<Department> enablelList = getEnableDeptList();
//        List<Department> list = DepartmentAlgorithm.findAllChild(parentId, enablelList);
//        if (list != null && !list.isEmpty()) {
//            for (Department dept : list) {
//                if (dept.getForService() == 1) {
//                    deptList.add(dept);
//                }
//            }
//        }
//        return deptList;
//    }

	public Department query(String id) {
		return getEnableDeptList().stream().filter(e -> e.getId().equals(id)).findFirst().get();
	}

	public boolean add(Department dept) {
		if (dept.getParentId() == null) {
			dept.setParentId("0");
		}
		if (dept.getStatus() == null) {
			dept.setStatus(2);
		}
		if (dept.getLevels() == null) {
			dept.setLevels(2);
		}
		int result = departmentDao.insert(dept);
		if (result < 0) {
			throw new AuthException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAllList();
		if (deptList != null && !deptList.isEmpty()) {
			SaManager.getSaTokenDao().update(deptKey(), JSON.toJSONString(deptList));
		}

		return true;
	}

	public boolean update(Department dept) {
		Department deptInfo = departmentDao.selectById(dept.getId());
		if (deptInfo == null) {
			throw new AuthException("未查询到部门信息");
		}
		BeanUtils.copyProperties(dept, deptInfo);
		int result = departmentDao.update(deptInfo);
		if (result < 0) {
			throw new AuthException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAllList();
		if (deptList != null && !deptList.isEmpty()) {
			SaManager.getSaTokenDao().update(deptKey(), JSON.toJSONString(deptList));
		}
		return true;
	}

	public boolean delete(String id) {
		Department deptInfo = departmentDao.selectById(id);
		if (deptInfo == null) {
			throw new AuthException("未查询到部门信息");
		}
		int result = departmentDao.delete(new Department().setId(deptInfo.getId()));
		if (result < 0) {
			throw new AuthException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAllList();
		if (deptList != null && !deptList.isEmpty()) {
			SaManager.getSaTokenDao().update(deptKey(), JSON.toJSONString(deptList));
		}
		return true;
	}

}
